library(tidyverse)
library(cjoint)
library(cregg)
library(xtable)


###SET WORKING DIRECTORY and LOAD FILES ----

set.seed(12345678)

df <- read.csv("b_data/3_long_data_for_analysis.csv")

df <- df %>% 
  mutate(immig_hist = 
           case_when(Q4_3 == 1 ~ "No history",
                     Q4_3 == 2 ~ "History of immig",
                     Q4_5_2 == 1 ~ "No history",
                     Q4_5_2 == 2 ~ "History of immig",
                     Q4_5_2 == 3 ~ "History of immig"))


###Making all variables factors ----
df$immig_hist <- as.factor(df$immig_hist)

df <- df %>% 
  mutate(Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                          "Medium man",   "Medium woman", "Brown man", 
                                          "Brown woman", "Black man", "Black woman"), 
                        ordered = is.ordered(Faces)))


df <- df %>%
  mutate(Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")))

df <- df %>%
  mutate(Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")))

df <- df %>%
  mutate(Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)))

df <- df %>%
  mutate(Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)))

df <- df %>%
  mutate(Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")))

df <- df %>%
  mutate(Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")))

df <- df %>%
  mutate(Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")))

df <- df %>%
  mutate(Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))

###Setting order of conjoint characteristics----
c_order <- c("Faces", "Identifies", "Religion", "Background", "Language",
             "Spouse", "Education", "Profession", "Political")

### Marginal mean differences of perceived americanness, by history of immigration (immigrant or child of immigrants) ----
american_immighist <- cregg::mm_diffs(na.omit(df),
                                 rating_r ~ Faces + Background
                                 + Education + Profession
                                 + Language + Political
                                 + Religion + Spouse
                                 + Identifies,
                                 by = ~ immig_hist,
                                 weights = ~weight, 
                                 feature_order = c_order)

### Figure A7---
american_immighist_p <- plot(american_immighist)

figure_A7 <- american_immighist_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between Those With and Without Immigration History",
                title = "Marginal Mean Differences in Perceived Americanness \n by Immigration History Among All Respondents")

ggsave("c_figures/figure_A7.pdf", width=8.5, height=11, plot=figure_A7)

### Table A14----
american_immighist_table <- american_immighist[c(5:9)]
print.xtable(xtable(american_immighist_table), "d_tables/table_A15.tex", type = "latex", include.rownames=FALSE)

#### Clear environment ----

rm(list = ls())

